package com.example.astronautcontact0123456789hwh1104;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ContactRVAdapter extends RecyclerView.Adapter<ContactRVAdapter.ViewHolder> {
    List<AstronautContact> contactList;
    public void setContactList(List<AstronautContact> contactList) {
        this.contactList = contactList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_contact, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AstronautContact contact = contactList.get(position);
        holder.tvItemRVName.setText(contact.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), ContactDetailActivity.class);
                intent.putExtra("id", contact.getId());
                view.getContext().startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return contactList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemRVName;
        public ViewHolder(View view)
        {
            super(view);
            tvItemRVName = view.findViewById(R.id.tvItemRVName);
        }
    }
}
